package org.weasel.wizards;

import java.sql.Time;
import java.util.ArrayList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.Wizard;
import org.weasel.business.Developer;
import org.weasel.business.Item;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.UserStory;
import org.weasel.business.Utilities;

public class UserStoryWizard extends Wizard{
	private UserStoryWizardPage1 wizardPage1;
	private UserStoryWizardPage2 wizardPage2;
	private ISelection selection;
	private Item item;
	private Project project;
	
	
	public UserStoryWizard(Item item, Project project) {
		super();
		this.item = item;
		this.project = project;
		setWindowTitle("Create new Userstory for this project ");
		setNeedsProgressMonitor(true);
	}
	
	@Override
	public void addPages() {
        wizardPage1 = new UserStoryWizardPage1(selection);
        addPage(wizardPage1);
        wizardPage2 = new UserStoryWizardPage2(selection, this.item, this.project);
        addPage(wizardPage2);
    }

	@Override
	public boolean performFinish() {
		String usName = wizardPage1.getUserstoryName();
		String usDescription = wizardPage1.getDescription();
		String usEstimatedEffort = wizardPage2.getEstimatedEffort();
		String usEstimatedRisk = wizardPage2.getEstimatedRisk();
		String usEstimatedTime = wizardPage2.getEstimatedTime();
		//TODO check what to do with the developers
		ArrayList<Developer> developers = wizardPage2.getDevelopers(); 
		
		try {
			UserStory userStory = new UserStory(Utilities.createUniqueId(), usName, Integer.parseInt(usEstimatedEffort), Time.valueOf(usEstimatedTime+":00:00"), Time.valueOf("00:00:00"), Integer.parseInt(usEstimatedEffort), developers, null);
			if(item instanceof Project) {
				Project project = (Project) item;
				project.getUnscheduled().addUnscheduledUserStory(userStory);
			} else if(item instanceof Release) {
				Release release = (Release) item;
				release.getUnscheduled().addUnscheduledUserStory(userStory);
			} else if(item instanceof Iteration) {
				Iteration iteration = (Iteration) item;
				iteration.addUserStory(userStory);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Utilities.callCreateAndUpdateTree();
		
		return true;
	}
}
